package com.new_wear.project.system.taskdo.controller;

import com.new_wear.common.utils.security.ShiroUtils;
import com.new_wear.framework.web.controller.BaseController;
import com.new_wear.framework.web.page.TableDataInfo;
import com.new_wear.project.system.dept.domain.Dept;
import com.new_wear.project.system.dept.service.IDeptService;
import com.new_wear.project.system.task.domain.CstTask;
import com.new_wear.project.system.task.service.ICstTaskService;
import com.new_wear.project.system.taskdo.domain.CstTaskDo;
import com.new_wear.project.system.taskdo.service.ICstTaskDoService;
import com.new_wear.project.system.user.domain.User;
import com.new_wear.project.system.user.service.IUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author ZhangRenpeng
 * @Description:
 * @date 2020/1/9 16:14
 */
@Controller
@RequestMapping("/system/myTask")
public class myTaskController extends BaseController {
    private String prefix = "system/myTask";

    @Autowired
    private ICstTaskDoService cstTaskDoService;
    @Autowired
    private IUserService userService;
    @Autowired
    private IDeptService deptService;
    @Autowired
    private ICstTaskService cstTaskService;

    /*我的任务--指派任务*/
    @RequiresPermissions("system:myTask:view")
    @GetMapping()
    public String myTask()
    {
        return prefix + "/myTask";
    }
    /*获取当前登录用户地区*/


    @RequiresPermissions("system:myTask:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo myTaskList(){
        Long userId = ShiroUtils.getSysUser().getUserId();
        User user = userService.selectUserById(userId);//查询用户信息
        CstTaskDo cdo = new CstTaskDo();
        cdo.setAreaId(user.getDeptId().intValue());
        List<CstTaskDo> list = cstTaskDoService.selectCstTaskDoList(cdo);
        if (list != null || list.size()>0){
            for (CstTaskDo cstTask:list) {
                Integer areaId = cstTask.getAreaId();
                Dept dept = deptService.selectDeptById(areaId.longValue());//地区
                cstTask.setDeptName(dept.getDeptName());
                //任务名称，id
                Integer tid = cstTask.getTid();
                CstTask task = cstTaskService.selectCstTaskById(tid);
                cstTask.settName(task.getTitle());
            }
        }

        return getDataTable(list);
    }
}
